import hashlib
import time

import scrapy
headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://www.mashangpa.com/problem-detail/1/",
    "sec-ch-ua": "\"Chromium\";v=\"142\", \"Microsoft Edge\";v=\"142\", \"Not_A Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0"
}
cookies = {
    "Hm_lvt_0d2227abf9548feda3b9cb6fddee26c0": "1761872091,1761968088,1762421683",
    "HMACCOUNT": "AC4951CBF00AD43B",
    "sessionid": "nvlwbkpsl9pey4cdqycdmoa587vgyani",
    "Hm_lpvt_0d2227abf9548feda3b9cb6fddee26c0": "1762422758"
}
url = "https://www.mashangpa.com/api/problem-detail/1/data/"
params = {
    "page": "1"
}
box = []
def md5_encrypt(text):
    """
    使用MD5算法对文本进行加密
    """
    md5_obj = hashlib.md5()
    md5_obj.update(text.encode('utf-8'))
    return md5_obj.hexdigest()





class mashangpa(scrapy.Spider):
    name = "mashang"

    def start_requests(self):
        for i in range(1,21):
            timestamp = int(time.time() * 1000)
            tuling = "tuling"
            Pagenumber = i
            token = md5_encrypt(tuling + str(timestamp)+str(Pagenumber))
            url = f"https://www.mashangpa.com/api/problem-detail/4/data/?page={i}&sign={token}&_ts={timestamp}"

            yield scrapy.Request(url=url, callback=self.parse,headers=headers,cookies=cookies)

    def parse(self, response):
        datas = response.json()
        for data in datas["current_array"]:
            box.append(data)
        print( box)
        i = 0
        for j in box:
            i += j
        print(i)





